package com.sgb.kjwl.viewmodel.self.bid;

import android.content.Intent;

import com.chuanglan.shanyan_sdk.OneKeyLoginManager;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.BidEntity;
import com.sgb.kjwl.model.entity.LoginEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.user.OneKeyLoginEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.bid.BidQuoteActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.SetPwdActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BidPriceModel extends BaseViewModel {

    private final BidQuoteActivity activity;
    private final SelfRepertory repertory;

    private int pageNumMaterial = 1;
    private int pageNumMachine = 1;
    private int pageNumGc = 1;
    private int pageNumLabor = 1;

    private int pageSize = 10;

    public BidPriceModel(BidQuoteActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getMaterial(boolean loadMore) {
        pageNumMaterial = loadMore ? pageNumMaterial + 1 : 1;
        APIManager.getInstance().getBidAPI().getMaterialList(pageNumMaterial, pageSize).enqueue(new Callback<BaseEntity<ListResponse<List<BidEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<BidEntity>>>> call, Response<BaseEntity<ListResponse<List<BidEntity>>>> response) {
                if (response.body() == null || !response.body().getState().equals("ok") || response.body().getData().getList().size() == 0) {
                    activity.emptyList();
                    return;
                }
                activity.showList(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<BidEntity>>>> call, Throwable t) {

            }
        });
    }

    public void getMachine(boolean loadMore) {
        pageNumMachine = loadMore ? pageNumMachine + 1 : 1;
        APIManager.getInstance().getBidAPI().getMachineList(pageNumMachine, pageSize).enqueue(new Callback<BaseEntity<ListResponse<List<BidEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<BidEntity>>>> call, Response<BaseEntity<ListResponse<List<BidEntity>>>> response) {
                if (response.body() == null || !response.body().getState().equals("ok") || response.body().getData().getList().size() == 0) {
                    activity.emptyList();
                    return;
                }
                activity.showList(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<BidEntity>>>> call, Throwable t) {

            }
        });

    }

    public void getGc(boolean loadMore) {
        pageNumGc = loadMore ? pageNumGc + 1 : 1;
        APIManager.getInstance().getBidAPI().getGcList(pageNumGc, pageSize).enqueue(new Callback<BaseEntity<ListResponse<List<BidEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<BidEntity>>>> call, Response<BaseEntity<ListResponse<List<BidEntity>>>> response) {
                if (response.body() == null || !response.body().getState().equals("ok") || response.body().getData().getList().size() == 0) {
                    activity.emptyList();
                    return;
                }
                activity.showList(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<BidEntity>>>> call, Throwable t) {
            }
        });

    }

    public void getLabor(boolean loadMore) {
        pageNumLabor = loadMore ? pageNumLabor + 1 : 1;
        APIManager.getInstance().getBidAPI().getLaborList(pageNumLabor, pageSize).enqueue(new Callback<BaseEntity<ListResponse<List<BidEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<BidEntity>>>> call, Response<BaseEntity<ListResponse<List<BidEntity>>>> response) {
                if (response.body() == null || !response.body().getState().equals("ok") || response.body().getData().getList().size() == 0) {
                    activity.emptyList();
                    return;
                }
                activity.showList(response.body().getData().getList());

            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<BidEntity>>>> call, Throwable t) {

            }
        });

    }
    public void oneKeyLogin(OneKeyLoginEntity oneKeyLoginEntity) {
        DialogHelper.showProgressDialog(activity, null, "登录中", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().oneClickLogin(oneKeyLoginEntity).enqueue(new Callback<BaseEntity<LoginEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LoginEntity>> call, Response<BaseEntity<LoginEntity>> response) {

                DialogHelper.dismissProgressDialog();
                if (response.body().getState() == null){
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    OneKeyLoginManager.getInstance().finishAuthActivity();
                    if (response.body().getData().isNeedRegister()){
                        Intent intent = new Intent(activity, SetPwdActivity.class);
                        intent.putExtra("mobile", response.body().getData().getPhone());
                        activity.startActivity(intent);
                    }else {
                        //登录成功逻辑
                        saveUserInfo(response.body().getData());
                        activity.loginSucceed(response.body().getData(), response.body().getData().getPhone());
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, response.body().getData().getToken());
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, response.body().getData().getPhone());
                        JPushInterface.setAlias(activity, 1, response.body().getData().getPhone());
                        getUserInfo();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LoginEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getUserInfo() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body() == null) return;
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.getUserMsgSucceed(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    private void saveUserInfo(LoginEntity loginEntity) {
        repertory.saveToken(loginEntity.getToken());
        repertory.saveAccount(loginEntity.getPhone());
    }
}
